import json

from django.conf import settings
from django.shortcuts import render,HttpResponse

from django.http.response import JsonResponse
from oneself.models import User,UserMenber
from django.contrib.auth import authenticate
from django.core.cache import cache
from oneself.Send.send_emial import sent_maill
from django.core.mail import send_mail
from home.Encrypt_with_password.zhi_password_Md5 import Random_Email
from home import restful
from oneself.models import QuestionSafety,QuestionSafetyTwo,SafetyAnswer,SafetyAnswerTwo
# from home.Encrypt_with_password.zhi_password_Md5 import Encrypt
# Create your views here.

#个人中心
def myself_index(request):
    username = request.COOKIES.get('username')
    print('username****=====****', username)
    if username is not None:
        print("username==", username)
        member_id = User.objects.get(username=username).member_id
        print("member_id=====", member_id)
        # user = User.objects.filter(username=username).all()
        member = UserMenber.objects.get(id=member_id).rember
        print("a===", member)
        return render(request, "person/index.html",{"member":member})

#个人资料
#个人信息
def myself_information(request):
    username = request.COOKIES.get('username')
    print('username****=====****', username)
    if username is not None:
        print("username==", username)
        member_id = User.objects.get(username=username).member_id
        print("member_id=====", member_id)
        user=User.objects.filter(username=username).all()
        member = UserMenber.objects.get(id=member_id).rember
        print("a===", member)
        return render(request, "person/information.html", {"member": member,"user1111":user})
    # return render(request, "person/information.html")
# 个人信息修改
def myself_information_handle(request):
    if request.method.lower()=='post':
        nick_name=request.POST.get('nickname')
        name=request.POST.get('tusername')
        sex = request.POST.get('radio10')
        birth = request.POST.get('birth')
        phone= request.POST.get('phone')
        email=request.POST.get('email')
        print('nicename===',nick_name)
        print('name===',name)
        print('sex===',sex)
        print('birth===',birth)
        print('phone===',phone)
        print('email===',email)
        if not all([nick_name,name,sex,birth,phone,email]):
            return render(request,'person/information.html',{"success":"信息不完整,请将信息填写完整后再进行修改"})
        username=request.COOKIES.get('username')
        user=User.objects.get(username=username)
        user.username=username
        user.name=name
        user.sex=sex
        user.birth=birth
        user.phone=phone
        user.email=email
        user.save()
        member_id = User.objects.get(username=username).member_id
        user = User.objects.filter(username=username).all()
        member = UserMenber.objects.get(id=member_id).rember
        return render(request,'person/information.html',{"success":"修改资料成功","member": member,"user1111":user})

#安全设置
def myself_safety(request):
    username = request.COOKIES.get('username')
    print('username****=====****', username)
    if username is not None:
        print("username==", username)
        member_id = User.objects.get(username=username).member_id
        print("member_id=====", member_id)
        user = User.objects.filter(username=username).all()
        member = UserMenber.objects.get(id=member_id).rember
        print("a===", member)
        user_all_info=User.objects.filter(username=username).all()
        return render(request, "person/safety.html",{"member":member,"user_info":user_all_info})

#支付密码
def myself_setpay(request):
    username=request.COOKIES.get('username')
    info=User.objects.get(username=username).phone
    return render(request, "person/setpay.html",{'phone':info})

def myself_setpay_handle(request):
    if request.method.lower()=='post':
        zpwd=request.POST.get('zpwd')
        zcpwd=request.POST.get('czpwd')
        username=request.COOKIES.get('username')
        user=User.objects.get(username=username)
        if zpwd!=zcpwd:
            return render(request,'person/setpay.html',{"suc":"您输入的两次密码不一致"})
        else:
            # info=Encrypt()
            # zcpwd=info.decrypt(key=15,s=zcpwd)
            user.nickname=zcpwd
            user.save()
            phone=User.objects.get(username=username).phone
            return render(request,'person/setpay.html',{"suc":"您的支付密码设置成功","phone":phone})

#修改密码
def myself_password(request):
    if request.method.lower()=='get':
        return render(request,'person/password.html')

    if request.method.lower()=='post':
        ypwd=request.POST.get('ypwd')
        npwd=request.POST.get('npwd')
        ncpwd=request.POST.get('ncpwd')
        uname=request.COOKIES.get('username')
        user = authenticate(username=uname, password=ypwd)
        if user:
            if not all([ypwd,npwd,ncpwd]):
                print("user===/////==",user)
                return render(request,'person/password.html',{"bai":"请您把信息填写完整"})
            if npwd!=ncpwd:
                print('两次输入的密码不一致')
                return render(request, 'person/password.html', {"bai": "两次输入的新密码不一致"})
            else:
                print("执行此处")
                print('hua===',User.objects.get(username=uname))
                print('ii==',User.objects.get(username=uname).password)
                user=User.objects.get(username=uname)
                user.set_password(ncpwd)
                user.save()
                return render(request, "person/password.html",{"bai":"密码修改成功"})

#绑定手机
def myself_bindphone(request):
    username=request.COOKIES.get('username')
    phone=User.objects.get(username=username).phone
    return render(request, "person/bindphone.html",{"phone":phone})
def myself_bindphone_handle(request):
    username=request.COOKIES.get('username')
    phone=User.objects.get(username=username).phone
    if request.method.lower()=='post':
        code=request.POST.get('code')
        nccode=request.POST.get('nccode')
        newphone=request.POST.get('newphone')
        if not all([code,nccode,newphone]):
            return render(request, "person/bindphone.html",{"phone":phone,"erop":"请将原手机号码下发的验证码、新的手机号码与新的手机号码下发的验证码填写完整！！"})
        username = request.COOKIES.get("username")
        if newphone==User.objects.get(username).phone:
            return render(request, 'person/bindphone.html', {"phone": phone, "erop": "该电话号码是您换绑前的电话号码，请不要重复绑定！！"})
        phone = User.objects.filter(username=username).all().values_list('phone',flat=True)[0]
        if newphone==phone:
            return render(request,'person/bindphone.html',{"phone":phone,"erop":"您绑定的电话号码已经存在，请您检查后再进行换绑！！"})
        user=User.objects.get(username=username)
        user.phone = newphone
        user.save()
        return render(request,'person/bindphone.html',{"phone":phone,"erop":"绑定新的手机号码成功"})


#绑定邮箱
def myself_email(request):
    return render(request, "person/email.html")

def send_email_user(request):
    username=request.COOKIES.get('username')
    print(username)
    code=Random_Email().ran()
    cache.set("code",code,36000)
    print("code:",cache.get('code'))
    email=request.GET.get("email")
    if email is None:
        print("发送失败")
    cache.set('email',email,36000)
    print('email',cache.get("email"))
    print('发送成功')
    sent_maill(email,username,code)
    return HttpResponse(json.dumps({"msg": "OK"}))


def check_email_user(request):
    email=request.GET.get('email')
    code=request.GET.get('code')
    print('缓存中是否包含:', cache.get("email"))
    print('取值:', cache.get("code"))
    cache_email=cache.get('email')
    codeaa=cache.get('code')
    if email==cache_email and code==codeaa:
        return restful.ok('Ok', data=None)
    else:
        return restful.params_error('验证码错误', data=None)

def myself_email_handle(request):
    if request.method.lower()=="post":
        email=request.POST.get('useremail')
        code=request.POST.get('yancode')
        if not all([email,code]):
            return render(request,'person/email.html',{'error':'请将邮箱和验证码填写完整'})

        username=request.COOKIES.get("username")
        email_u=User.objects.filter(email=email).all().values_list('email',flat=True)[0]
        print(email_u)
        if email==User.objects.get(username=username).email:
            return render(request,'person/email.html',{'error':'该邮箱您已经绑定过了，请你不要重复绑定'})
        if email_u==email:
            return render(request,'person/email.html',{'error':'邮箱已经存在,请你重新绑定'})
        user=User.objects.get(username=username)
        user.email=email
        user.save()
        return render(request,'person/email.html',{'error':'邮箱绑定成功'})

    # return render(request, "person/email.html")

#实名认证
def myself_idcard(request):
    return render(request, "person/idcard.html")

#设置安全问题
def myself_question(request):
    question=QuestionSafety.objects.all()
    questiontwo=QuestionSafetyTwo.objects.all()
    return render(request, "person/question.html",{"question":question,"questiontwo":questiontwo})

def myself_question_handle(request):
    question=QuestionSafety.objects.all()
    questiontwo=QuestionSafetyTwo.objects.all()
    if request.method.lower()=="post":
        ques=request.POST.get('s1')
        ques2=request.POST.get('s2')
        answer=request.POST.get('answer1')
        answer2=request.POST.get('answer2')
        if not all([ques,ques2,answer,answer2]):
            return render(request, "person/question.html", {"question": question, "questiontwo": questiontwo,"error":"请将问题和信息填写完整"})
        print("ques",ques)
        print("ques2",ques2)
        print("answer",answer)
        print("answer2",answer2)
        username=request.COOKIES.get('username')
        userid=User.objects.get(username=username).id
        SafetyAnswer.objects.create(answer=answer,questionid_id=ques,userid_id=userid)
        SafetyAnswerTwo.objects.create(answertwo=answer2,questiontwoid_id=ques2,userid_id=userid)
        return render(request, "person/question.html",{"question":question,"questiontwo":questiontwo,"error":"安全问题设置成功"})

#收货地址
def myself_address(request):
    return render(request, "person/address.html")

#我的交易
#订单管理
def myself_order(request):
    return render(request, "person/order.html")

#订单详情
def myself_orderinfo(request):
    return render(request, "person/orderinfo.html")

#退换货
def myself_refund(request):
    return render(request, "person/refund.html")

#查看物流
def myself_logistics(request):
    return render(request, "person/logistics.html")

#发表评价
def myself_commentlist(request):
    return render(request, "person/commentlist.html")

#退款售后
def myself_change(request):
    return render(request, "person/change.html")

#钱款去向
def myself_record(request):
    return render(request, "person/record.html")

#我的资产
#优惠券
def myself_coupon(request):
    return render(request, "person/coupon.html")

#红包
def myself_bonus(request):
    return render(request, "person/bonus.html")

#账单明细
def myself_bill(request):
    return render(request, "person/bill.html")

def myself_billlist(request):
    return render(request, "person/billlist.html")

#我的小屋
#收藏
def myself_collection(request):
    return render(request, "person/collection.html")

#足迹
def myself_foot(request):
    return render(request, "person/foot.html")

#评价
def myself_comment(request):
    return render(request, "person/comment.html")

#消息
def myself_news(request):
    return render(request, "person/news.html")

#消息详情
def myself_blog(request):
    return render(request, "person/blog.html")

